package com.waterworld.watch.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.waterworld.watch.R;

import java.util.List;
import java.util.Random;

/**
 * Created by nhuan
 * Time:2019/3/8.
 */

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.MyHolder> {
    private Context mContext;
    private List<String> mData;
    private int[] colors = {R.drawable.curriculum_color_1,R.drawable.curriculum_color_2,R.drawable.curriculum_color_3,R.drawable.curriculum_color_4,R.drawable.curriculum_color_5,R.drawable.curriculum_color_6,R.drawable.curriculum_color_7,R.drawable.curriculum_color_8};

    public CourseAdapter(Context mContext, List<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    public List<String> getData(){
        return mData;
    }
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.item_course, viewGroup, false);
        itemView.getLayoutParams().height = viewGroup.getHeight()/8;
        itemView.getLayoutParams().width = viewGroup.getWidth()/5;
        return new CourseAdapter.MyHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.btn_course.setText(mData.get(i));
        Random random = new Random();
        myHolder.btn_course.setBackground(mContext.getResources().getDrawable(colors[random.nextInt(8)]));
        if (listener != null) {
            myHolder.btn_course.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = myHolder.getLayoutPosition();
                    listener.onClick(view, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private Button btn_course;

        MyHolder(@NonNull View itemView) {
            super(itemView);
            btn_course = itemView.findViewById(R.id.btn_course);

        }
    }

        private CourseAdapter.SetonClickListener listener;

        public void setOnItemClick(CourseAdapter.SetonClickListener listener) {
            this.listener = listener;
        }

    public interface SetonClickListener {
        void onClick(View view, int position);
    }
}
